package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.blb.entity.Dept;
import com.blb.entity.ProductionLine;
import com.blb.entity.User;
import com.blb.service.IDeptService;
import com.blb.service.IProductionLineService;

import com.blb.utils.UUIDUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *生产管理的生产线产能设置
 * @author xiaoxiong
 * @since 2020-05-20
 */
@Controller
@RequestMapping("/production")
public class ProductionLineController {

    @Autowired
    IProductionLineService iProductionLineService;

    @Resource
    IDeptService iDeptService;
    @RequiresPermissions("productionline")
    @GetMapping("/sc_cn")
    @ResponseBody
    public Map<String,Object> list(Model model){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Map<String,Object> map = new LinkedHashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("data",iProductionLineService.list(new QueryWrapper<ProductionLine>().eq("company_id",user.getCompanyId())));
        return map;
    }
    @RequiresPermissions("productionline")
    @RequestMapping("/sc_add")
    @ResponseBody
    public String add(ProductionLine productionLine){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        productionLine.setId(UUIDUtils.id());
        productionLine.setCreateTime(new Date());
        productionLine.setCompanyId(user.getCompanyId());
        iProductionLineService.save(productionLine);
        return "1";
    }
    @RequiresPermissions("productionline")
    @RequestMapping("/sc_upp")
    @ResponseBody
    public  String update(ProductionLine productionLine){
        iProductionLineService.update(productionLine,new UpdateWrapper<ProductionLine>().eq("id",productionLine.getId()));
        return "1";
    }
    @RequiresPermissions("productionline")
    @RequestMapping("/sc_del")
    @ResponseBody
    public  String del(String id){
        iProductionLineService.removeById(id);
        return "1";
    }
    @RequiresPermissions("productionline")
    @RequestMapping("/dept")
    public  String dept(Model model){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Dept one = iDeptService.getOne(new QueryWrapper<Dept>().eq("dept_name", "生产部").eq("company_id",user.getCompanyId()));
        String id;
        if (one==null){
            id = ".";
        }else {
            id = one.getId();
        }
        List<Dept> list = iDeptService.list(new QueryWrapper<Dept>().eq("pid",id));
        model.addAttribute("list",list);
        return "production/sccn/sc_add";
    }
    @RequiresPermissions("productionline")
    @RequestMapping("/depts")
    public  String depts(Model model){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Dept one = iDeptService.getOne(new QueryWrapper<Dept>().eq("dept_name", "生产部").eq("company_id",user.getCompanyId()));
        String id;
        if (one==null){
            id = ".";
        }else {
            id = one.getId();
        }
        List<Dept> list = iDeptService.list(new QueryWrapper<Dept>().eq("pid",id));
        model.addAttribute("list",list);
        return "production/sccn/sc_upp";
    }
}

